<?php

namespace app\common\controller\zhongzhuanv2;

use app\admin\library\Auth;
use think\Config;
use think\Controller;
use think\Db;
use ec\Ecbase as Eccommon;

/**
 * 后台控制器基类
 */
class Autoassigncd extends Controller{
    
    
    /**
     * 
     * @param type $request_data
     * $list = [
            array(
                "followUserId" => ,
                "mobile" => 18300000015,
                "name" => "测试数据15",
                "fields"=>[
                    '82705932'=>'C级(明确拒绝)',
                    '82720068'=>'https://byrobot-prod.oss-cn-hangzhou.aliyuncs.com/20240807/20240807NbT05l8528034362_19553058820.mp3'
                ]
            )
        ];
     */
    public static function startAssign($request_data){
        
        $timestamp = strtotime("today");
        $limit = empty($request_data['limit']) ? 200 : $request_data['limit'];
        $field_id_config = config('zhongzhuan.field');
        $yixiang_field_id = $field_id_config['yixiang']['fieldid'];
        $baiying_luyin_field_id = $field_id_config['baiying_luyin']['fieldid'];
        $baiying_luyin_duration_field_id = $field_id_config['baiying_luyin_duration']['fieldid'];
        
        $zhiying_publicpond  = config('zhongzhuan.zhiying_publicpond');
        $zhiying_channelId= config('zhongzhuan.zhiying_channelId');
        
        
        $assign_id = empty($request_data['assign_id'])?1:$request_data['assign_id'];
        $gonghai = ['C','D'];
        $kehu_list = db(getBaiYingDataTable())->field('id byid,callJobId,customerTelephone,customerName,callInstanceId,yixiang_txt,yixiang_value,startTime,duration')->where([
                'yixiang_value'=>['in',$gonghai],
                'status'=>0,
                'duration'=>['>',12],
                'startTime'=>['>',$timestamp * 1000],
            ])->limit($limit)->order('startTime asc')->select();
       
       $luyin_domain_prefix = config('zhongzhuan.luyin_domain_prefix');
       if(empty($kehu_list)){
           return ;
       }
        
        $assign_end_list = [];
        foreach ($kehu_list as $kehu_item){
            
            $luyinOssUrl = $luyin_domain_prefix.'?callInstanceId='.$kehu_item['callInstanceId'];
            
            $duration = empty($kehu_item['duration']) ? 0 : $kehu_item['duration'];
            
            $kehu_item_name = $kehu_item['customerName'];
            $kehu_item_name = empty($kehu_item_name) ? '未命名' .substr($kehu_item['customerTelephone'], -4) : $kehu_item_name;
        
            $temp_data = [
                "mobile"=>$kehu_item['customerTelephone'],"name"=>$kehu_item_name,'publicPondId'=>$zhiying_publicpond,"channelId"=>$zhiying_channelId,
                "fields"=>[$yixiang_field_id=>$kehu_item['yixiang_txt'],$baiying_luyin_field_id=>$luyinOssUrl,$baiying_luyin_duration_field_id=>$duration],
                
            ];
            
            $startTime = strlen($kehu_item['startTime']) > 10 ?  $kehu_item['startTime'] / 1000 : $kehu_item['startTime'];

            $middle_data = [
                "callJobId"=>$kehu_item['callJobId'],"mobile"=>$kehu_item['customerTelephone'],"name"=>$kehu_item_name,"createtime"=>time(),'yixiang_value'=>$kehu_item['yixiang_value'],
                'byid'=>$kehu_item['byid'],'yixiang_txt'=>$kehu_item['yixiang_txt'],"starttime"=>date('Y-m-d H:i:s',$startTime),
                "content"=> json_encode($temp_data,JSON_UNESCAPED_UNICODE)
            ];
            
            $assign_end_list[] = $middle_data;
            
            
        }
        
        
        
        $all_kehu_id = array_column($assign_end_list, 'byid');
       
        
        try{
            Db::startTrans();
            
//            
//            db('ec_assign_kehu')->where('id',$assign_id)->update([
//                'status'=>1,
//                'daituisong_count'=>count($all_kehu_id),
//            ]);
            db('ec_aiuser_to_ecuser')->insertAll($assign_end_list);
            db(getBaiYingDataTable())->where('id','in',$all_kehu_id)->setField('status',1);
            
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
        }
        
    }
    
}
